package cn.easybuy.web.backend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.easybuy.entity.User;
import cn.easybuy.service.user.UserService;
import cn.easybuy.service.user.UserServiceImpl;
import cn.easybuy.utils.SecurityUtils;
import cn.easybuy.utils.UserPage;

public class UserListServlet extends HttpServlet {


	public UserListServlet() {
		super();
	}



	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if(action.equals("pageShow")){
			UserPage page = new UserPage();
			String pageNo = request.getParameter("pageNo");
			if(pageNo==null){
				pageNo="1";
			}
			UserService us = new UserServiceImpl();

			page.setPageNo(Integer.parseInt(pageNo));
			int pageSize = 5;
			page.setPageSize(pageSize); //设置分页数据量
			page.setDataNum(us.getDataNum());
			page.setList(us.getPageList(Integer.parseInt(pageNo), pageSize));
			request.setAttribute("page", page);
			request.getRequestDispatcher("Member_UserList.jsp").forward(request, response);


		}else if(action.equals("update")){
			String id=request.getParameter("id");
			UserService us=new UserServiceImpl();
			User user=us.findToID(id);
			request.setAttribute("user", user);
			request.getRequestDispatcher("Member_UpdateUser.jsp").forward(request,response);
		}else if(action.equals("updateSubmit")){

			//获取表单中的数据
			String id = request.getParameter("id");
			String loginName=request.getParameter("l_user");
			String userName=request.getParameter("l_mem");
			String idCode=request.getParameter("l_num");
			String email=request.getParameter("l_email");
			String phone=request.getParameter("l_phone");
			String type=request.getParameter("l_type");
			//把数据封装到用户实体
			User user=new User();
			user.setId(Integer.parseInt(id));
			user.setLoginName(loginName);
			user.setUserName(userName);
			user.setMobile(phone);
			user.setEmail(email);
			user.setIdentityCode(idCode);
			user.setType(Integer.parseInt(type));

			//创建UserService对象调用Update方法
			UserService us = new UserServiceImpl();
			int i = us.updateUser(user);
			if(i>0){
				out.print("<script>");
				out.print("alert('修改成功!');");
				out.print("window.location.href='UserListServlet?action=pageShow';");
				out.print("</script>");
			}else{
				out.print("<script>");
				out.print("alert('修改失败!');");
				out.print("window.location.href='UserListServlet?action=pageShow';");
				out.print("</script>");
			}
		}else if(action.equals("AddUser")){
			//获取参数
			String id = request.getParameter("id");
			String loginName=request.getParameter("l_user");
			String userName=request.getParameter("l_mem");
			String idCode=request.getParameter("l_num");
			String password = request.getParameter("l_password");
			String email=request.getParameter("l_email");
			String phone=request.getParameter("l_phone");
			String type=request.getParameter("l_type");
			int	sex = Integer.parseInt(request.getParameter("l_sex"));
			//封装实体
			User user = new User();
			user.setUserName(userName);
			user.setLoginName(loginName);
			user.setPassword(SecurityUtils.md5Hex3(password));
			user.setSex(sex);
			user.setEmail(email);
			user.setMobile(phone);
			user.setIdentityCode(idCode);
			user.setType(Integer.parseInt(type));

			//创建对象 调用register方法 
			UserService userService = new UserServiceImpl();
			int num = userService.registerUser(user);
			if(num>0){
				out.print("<script>");
				out.print("alert('新增成功!');");
				out.print("window.location.href='UserListServlet?action=pageShow';");
				out.print("</script>");
			}else{
				out.print("<script>");
				out.print("alert('新增失败!');");
				out.print("window.location.href='UserListServlet?action=pageShow';");
				out.print("</script>");
			}
		}else if(action.equals("del")){
			String id = request.getParameter("id");
			UserService us = new UserServiceImpl();
			int i = us.delUser(Integer.parseInt(id));
			if(i>0){
				out.print("<script>");
				out.print("alert('删除成功!');");
				out.print("window.location.href='UserListServlet?action=pageShow';");
				out.print("</script>");
			}else{
				out.print("<script>");
				out.print("alert('删除失败!');");
				out.print("window.location.href='UserListServlet?action=pageShow';");
				out.print("</script>");
			}
		}
	}

}
